package analyse;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import com.stock.dao.Averline;

import data.Constant;
import dataconnect.ConnectionFactory;

public class PriceIncreAlyz extends AbstractAlyzer{
	
	public PriceIncreAlyz() {}

	@Override
	DealDayData alanalyze(List<DealDayData> dealDatas) {
		BigDecimal priceBat = new BigDecimal("1");//除权
		BigDecimal maxPrice = new BigDecimal("-1");
		
		BigDecimal oldMinPrice = new BigDecimal("0");//除权
		BigDecimal oldMaxPrice = new BigDecimal("0");
		
		DealDayData result = new DealDayData();
		
		BigDecimal dayPercentWave = BigDecimal.ZERO;
		
		boolean upSign = false;//连跌天数
		
		BigDecimal fiveDayPrice = BigDecimal.ZERO;
		BigDecimal tenDayPrice = BigDecimal.ZERO;
		
		int upDays = 0;
		
		Connection con = ConnectionFactory.getConnection();
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt = con.createStatement();
			rs = stmt.executeQuery("select priceLevel,minprice,maxPice from stockanlyz where number = '"+dealDatas.get(0).getNumber()+"'");
			while (rs.next()) {
				oldMinPrice = rs.getBigDecimal(2);
				oldMaxPrice = rs.getBigDecimal(3);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(rs!=null){
					rs.close();
				}
				if(stmt!=null){
					stmt.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		for (DealDayData dealDayData : dealDatas) {
			if(dealDayData.getRatePercent().compareTo(new BigDecimal("-10.5"))<0){
				priceBat = priceBat.add(dealDayData.getRatePercent().divide(Constant.ONEHUNDRED));
			}
			BigDecimal price = dealDayData.getPrice();
			price = price.multiply(priceBat).setScale(2,BigDecimal.ROUND_HALF_UP);
			dealDayData.setPrice(price);
			
			if(maxPrice.compareTo(dealDayData.getPrice())<0){
				maxPrice = dealDayData.getPrice();
				result = dealDayData;
			}
			
			if(!upSign&&dealDayData.getRatePercent().compareTo(Constant.ZERO)<0){
				upDays = dealDayData.getSeq()-1;
				upSign = true;
			}
			dayPercentWave = dayPercentWave.add(dealDayData.getRatePercent().abs());
			
			if(dealDayData.getSeq()<=10){
				tenDayPrice = tenDayPrice.add(dealDayData.getPrice());
				if(dealDayData.getSeq()<=5){
					fiveDayPrice = fiveDayPrice.add(dealDayData.getPrice());
				}
			}
			
		}
		
		/*
		 * 
		 */
		if(priceBat.compareTo(new BigDecimal("1"))!=0){
			result.setPriceDeal(true);
		}
		
		result.setUpDays(upDays);
		result.setDayPercentWave(dayPercentWave);
		result.setFiveDayPrice(fiveDayPrice.divide(Constant.FIVE, 2, BigDecimal.ROUND_HALF_UP));
		result.setTenDayPrice(tenDayPrice.divide(Constant.TEN, 2, BigDecimal.ROUND_HALF_UP));
		
		if(result.getMaxPrice().subtract(dealDatas.get(0).getPrice()).divide(result.getMaxPrice(), 2,BigDecimal.ROUND_HALF_UP)
				.compareTo(new BigDecimal(0.05))<0){
			result.setPriceLevel(1);
		}
		
		return result;
	}

}
